package com.lxyk.service.impl;

import com.lxyk.mapper.OrderMapper;
import com.lxyk.pojo.Order;
import com.lxyk.service.AccountService;
import com.lxyk.service.OrderService;
import com.lxyk.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @ClassName OrderServiceImpl
 * @Description TODO
 * @Author FJQ
 * @Date 2022/11/30 15:56
 * @Version 1.0
 **/
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    //这里是所有的核心
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private AccountService accountService;
    @Resource
    private StorageService storageService;

    //创建订单->调用库存服务扣减库存->调用账户服务扣减账户余额->修改订单状态
    @Override
    @GlobalTransactional(name = "fsp-create-order",rollbackFor = Exception.class)
    public void create(Order order) {
        log.info("---->开始新建订单");
        //新建订单
        orderMapper.create(order);

        //扣减库存
        log.info("---->订单微服务开始调用库存，做扣减count");
        storageService.decrease(order.getProductId(),order.getCount());
        log.info("---->订单微服务开始调用库存，做扣减end");

        //扣减账户余额
        log.info("---->订单微服务开始调用账户，做扣减money");
        accountService.decrease(order.getUserId(),order.getMoney());
        log.info("---->订单微服务开始调用账户，做扣减end");

        //修改订单状态   从0到1表示已完成
        log.info("---->修改订单状态开始");
        orderMapper.update(order.getUserId(),0);
        log.info("---->修改订单状态完成");
        log.info("---->下订单结束！！！！");


    }
}
